package com.gl.rent;


import cn.hutool.core.convert.Convert;
import com.gl.common.util.SecurityUtils;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.HousingAllocation;
import com.haixiaoke.saas.repository.service.IHousingAllocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 房屋配置Controller
 *
 * @author qixi
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/housing/allocation")
public class HousingAllocationController extends BaseController {
    @Autowired
    private IHousingAllocationService housingAllocationService;


    /**
     * 获取房屋配置详细信息
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:allocation:query')")
    @GetMapping(value = "/{allocationId}")
    public AjaxResult getInfo(@PathVariable("allocationId") Long allocationId) {
        return success(housingAllocationService.selectHousingAllocationByAllocationId(allocationId));
    }

    /**
     * 新增房屋配置
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:allocation:add')")
    @PostMapping
    public AjaxResult add(@Valid  @RequestBody HousingAllocation housingAllocation) {
        //填写创建人
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        housingAllocation.setCreateBy(id);
        if (!housingAllocationService.checkHouseIdUnique(housingAllocation.getHouseId())){
            return  error("房屋配置已添加过不可重复添加！");
        }

        return toAjax(housingAllocationService.insertHousingAllocation(housingAllocation));
    }

    /**
     * 修改房屋配置
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:allocation:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody HousingAllocation housingAllocation) {
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        housingAllocation.setUpdateBy(id);
        return toAjax(housingAllocationService.updateHousingAllocation(housingAllocation));
    }

    /**
     * 修改房屋配置上下架状态
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:allocation:edit')")
    @PutMapping("/updateState")
    public AjaxResult editState(@RequestBody HousingAllocation housingAllocation) {
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        housingAllocation.setUpdateBy(id);
        return toAjax(housingAllocationService.updateHousingAllocationState(housingAllocation));
    }

    /**
     * 删除房屋配置
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:allocation:remove')")
    @DeleteMapping("/{allocationIds}")
    public AjaxResult remove(@PathVariable Long[] allocationIds) {
        return toAjax(housingAllocationService.deleteHousingAllocationByAllocationIds(allocationIds));
    }
}
